package team.sun.sss.controller.hardwork;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import team.sun.sss.dao.hardwork.HwApplyDao;
import team.sun.sss.dao.hardwork.HwBatchDao;
import team.sun.sss.dao.hardwork.HwLaborDao;
import team.sun.sss.dao.hardwork.HwTimeDao;
import team.sun.sss.dao.student.FamilyDao1;
import team.sun.sss.dao.student.StudentDao;
import team.sun.sss.pojo.hardwork.HwBatch;
import team.sun.sss.pojo.hardwork.HwLabor;

import java.util.List;

@Controller
public class HwBatchController {
    @Autowired
    HwApplyDao hwApplyDao;
    @Autowired
    StudentDao studentDao;
    @Autowired
    FamilyDao1 familyDao1;
    @Autowired
    HwBatchDao hwBatchDao;
    @Autowired
    HwLaborDao hwLaborDao;
    @Autowired
    HwTimeDao hwTimeDao;
    @RequestMapping("/hw_staff_batch")
    public String staffBatch(Model model,@RequestParam(required = false,defaultValue="1",value="pageNum")Integer pageNum,
                             @RequestParam(defaultValue="5",value="pageSize")Integer pageSize){
        if(pageNum == null){
            pageNum = 1;   //设置默认当前页
        }
        if(pageNum <= 0){
            pageNum = 1;
        }
        if(pageSize == null){
            pageSize = 5;    //设置默认每页显示的数据数
        }
        //1.引入分页插件,pageNum是第几页，pageSize是每页显示多少条,默认查询总数count
        PageHelper.startPage(pageNum,pageSize);
        //2.紧跟的查询就是一个分页查询-必须紧跟.后面的其他查询不会被分页，除非再次调用PageHelper.startPage
        try {
            List<HwBatch> studentList = hwBatchDao.getHwBatchList();
            model.addAttribute("HwBatchList",studentList);
            //3.使用PageInfo包装查询后的结果,5是连续显示的条数,结果list类型是Page<E>
            System.out.println("分页数据："+studentList);
            PageInfo<HwBatch> pageInfo = new PageInfo<HwBatch>(studentList,pageSize);
            //4.使用model/map/modelandview等带回前端
            model.addAttribute("pageInfo",pageInfo);
        }finally {
            PageHelper.clearPage(); //清理 ThreadLocal 存储的分页参数,保证线程安全
        }

        model.addAttribute("teamList",hwBatchDao.getTeamName());
        return "hardWork/hw_staff_batch";
    }
    @RequestMapping("/staff_select_batch")
    public String staffSelectBatch(Model model,String batchTeam,String state){
        model.addAttribute("HwBatchList",hwBatchDao.getBatchByTeam(batchTeam,state));
        model.addAttribute("teamList",hwBatchDao.getTeamName());
        return "hardWork/hw_staff_batch";
    }
    @RequestMapping("/to_hw_staff_writeBatch")
    public String tostaffWriteBatch(Model model ){
        model.addAttribute("hwBatch",new HwBatch());
        return "hardWork/hw_staff_batchWrite";

    }
    @RequestMapping("/hw_staff_writeBatch")
    public String staffWriteBatch(Model model, @ModelAttribute(value = "hwBatch") HwBatch hwBatch ,String batchTeam, String state,
                                  String startTime, String endTime, String submitTime,  String ApplyStartTime, String applyEndTime){
        hwBatchDao.addHwBatch(batchTeam,state,startTime,endTime,submitTime,ApplyStartTime,applyEndTime);
        model.addAttribute("HwBatchList",hwBatchDao.getHwBatchList());
        return "hardWork/hw_staff_batch";
    }
    @RequestMapping("/hw_staff_batchDetail/{num}")
    public String staffBatchDetail(Model model, @PathVariable("num") Integer num){

        model.addAttribute("HwBatchDetail",hwBatchDao.getBatchByNum(num));
        return "hardWork/hw_staff_batchDetail";
    }
    //修改
    @RequestMapping("/to_hw_staff_batchUpdate/{num}")
    public String toStaffBatchUpdate(Model model, @PathVariable("num") Integer num){
        model.addAttribute("HwBatchDetail",hwBatchDao.getBatchByNum(num));
        return "hardWork/hw_staff_batchUpdate";
    }
    @RequestMapping("/hw_staff_batchUpdate")
    public String staffBatchUpdate(Model model, @ModelAttribute(value = "hwBatch") HwBatch hwBatch ,String batchTeam, String state,
                                  String startTime, String endTime, String submitTime, String applyStartTime, String applyEndTime,
                                   Integer batch_num){
        hwBatchDao.updateHwBatch(batchTeam,state,startTime,endTime,submitTime,applyStartTime,applyEndTime,batch_num);
        model.addAttribute("HwBatchList",hwBatchDao.getHwBatchList());
        return "hardWork/hw_staff_batch";
    }
    //删除

    @RequestMapping("/hw_staff_batchDelete/{num}")
    public String staffBatchDelete(Model model, @PathVariable("num") Integer num){
        hwBatchDao.deleteHwBatch(num);

        model.addAttribute("HwBatchList",hwBatchDao.getHwBatchList());
        return "hardWork/hw_staff_batch";
    }
    @RequestMapping("/hw_unit_batch")
    public String acceptBatch(Model model){
        //学工处显示学生的申请表
        List<HwBatch> getHwBatchList= hwBatchDao.getHwBatchList();
        //批次表
        model.addAttribute("HwBatchList2",getHwBatchList);

        return "hardWork/hw_unit_batch";

    }

}
